namespace EyeOpen.Validation
{
    using System;

    /// <summary>
    /// Defines a base class for a validator.
    /// </summary>
    /// <typeparam name="T">The </typeparam>
    public abstract class Validator<T> : IValidator<T>
    {
        /// <summary>
        /// Gets the validation message.
        /// </summary>
        /// <value>The validation message.</value>
        public abstract string ValidationMessage
        {
            get;
        }

        /// <summary>
        /// Gets a value indicating whether this instance is validation called.
        /// </summary>
        /// <value>
        /// <c>true</c> if this instance is validation called; otherwise, <c>false</c>.
        /// </value>
        protected bool IsValidationCalled
        {
            get;
            private set;
        }

        /// <summary>
        /// Determines whether the specified object is valid.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>
        /// <c>true</c> if the specified object is valid; otherwise, <c>false</c>.
        /// </returns>
        public bool IsValid(object obj)
        {
            return IsValid((T)obj);
        }

        /// <summary>
        /// Determines whether the specified obj is valid.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <returns>
        /// <c>true</c> if the specified obj is valid; otherwise, <c>false</c>.
        /// </returns>
        public abstract bool IsValid(T obj);

        /// <summary>
        /// Set validation is called.
        /// </summary>
        protected void ValidationCalled()
        {
            IsValidationCalled = true;    
        }

        /// <summary>
        /// Checks if validation is called otherwise throws an exception.
        /// </summary>
        protected void CheckValidationIsCalled()
        {
            if (!IsValidationCalled)
            {
                throw new InvalidOperationException("IsValid method was not called.");
            }
        }
    }
}